<?php

class WidgetFormHiddenInfo extends sfWidgetForm
{

	public function configure($options = array(), $attributes = array())
	{
		$this->addRequiredOption('model');
		$this->addRequiredOption('column');
		$this->addOption('column_separator', ' ');
		$this->addOption('query_column', 'id');
		$this->addOption('display_value', null);
	}

	private function getInfo($value)
	{
		$column = $this->getOption('column');

		if (!is_array($column))
			$column = array($column);

		$query = Doctrine_Core::getTable($this->getOption('model'))
			->createQuery()
			->select(implode(',', $column))
			->where($this->getOption('query_column') . ' = ?', $value)
			->fetchArray();

		if (isset($query[0]))
		{
			$value = '';
			foreach ($column as $c)
				$value = $value . $query[0][$c] . $this->getOption('column_separator');

			return $value;
		}

		return 'No válido';
	}

	public function render($name, $value = null, $attributes = array(), $errors = array())
	{
		$info = $this->getOption('display_value');

		if ($info == null)
			$info = $this->getInfo($value);

		return $this->renderTag('input', array('type' => 'hidden', 'name' => $name, 'value' => $value))
			. '<strong>' . $info . '</strong>';
	} 

}
